package com.example.controller;

import com.example.entity.result.Result;
import com.example.entity.result.ResultBean;
import com.example.entity.vo.Car;
import com.example.entity.vo.User;
import com.example.exception.OperatorException;
import com.example.service.CarService;
import com.example.service.UserService;
import com.example.utils.DataUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 个人中心
 */
@RestController
@RequestMapping("/mine")
public class MineController extends BaseHandleException {
    @Autowired
    private UserService userService;
    @Autowired
    private CarService carService;

    //更新
    @RequestMapping("/update")
    public ResultBean updateUser(@RequestParam("user") String json) {
        ObjectMapper objectMapper = DataUtils.getObjectMapper();
        try {
            User user = objectMapper.readValue(json, User.class);
            return DataUtils.getResultBean(userService.updateUser(user), "");
        } catch (JsonProcessingException e) {
            System.out.println(e.getMessage());
        }
        return DataUtils.getResultBean(Result.ERROR, "", "上传失败");
    }

    //删除
    @RequestMapping("/delete")
    public ResultBean delete(@RequestParam("id") String json) {
        int id = DataUtils.paramToInteger(json);
        Result result = userService.deleteUser(id);
        return DataUtils.getResultBean(result, "");
    }

    //充值
    @RequestMapping("/pay")
    public ResultBean pay(@RequestParam("user") String json,
                          @RequestParam("way") String key,
                          @RequestParam("value") String value) {

        float v = DataUtils.paramToFloat(value);
        return userService.pay(DataUtils.getUser(json), key, v);
    }

    //发布车辆
    @RequestMapping("/release")
    public ResultBean releaseCar(@RequestParam("car") String json) {
        if (json != null && !json.isEmpty()) {
            ObjectMapper objectMapper = DataUtils.getObjectMapper();
            try {
                Car car = objectMapper.readValue(json, Car.class);
                return DataUtils.getResultBean(carService.insertCar(car));
//                return DataUtils.getResultBean(Result.SUCCESS,"");
            } catch (JsonProcessingException e) {
                throw new OperatorException(e.getMessage(), Result.ERROR);
            }
        } else {
            return DataUtils.getResultBean(Result.PARAMETER_EMPTY, "", "");
        }
    }

    @RequestMapping("updateState")
    public ResultBean updateState(@RequestParam("cid") String cid, @RequestParam("state") String state) {
        Result result = carService.updateState(DataUtils.paramToInteger(cid), DataUtils.paramToInteger(state));
        return DataUtils.getResultBean(result, "");
    }
}
